package com.zhiyou100.api.io_demo;

import java.io.*;

public class Demo3 {

    public static void main(String[] args) throws IOException {
        copy();
    }


    //利用缓冲流将file文件中内容拷贝到file2文件中
    public static void copy(){
        File srcFile = new File("./java_api/file.txt");
        File targetFile = new File("./java_api/file2.txt");
        try {
            //包装一个字符缓冲输入流
            FileReader reader = new FileReader(srcFile);
            BufferedReader br = new BufferedReader(reader);
            //包装一个字符缓冲输出流
            FileWriter writer = new FileWriter(targetFile);
            BufferedWriter bw = new BufferedWriter(writer);
            //读取一行数据   readLine
            String s = br.readLine();
            //不为空就循环读
            while (s != null){
                bw.write(s);
                //写入一个换行符    newLine
                bw.newLine();
                s = br.readLine();
            }
            bw.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}
